
global filename "regs_birthhist"
cap erase "${filename}.csv"

use "birthhist", clear

do "edit_beforeregs.do"

foreach var in pctag  {
g `var'_a365=`var'
g `var'_270=`var'
}
g pctagindex=pctag*index


ds *temp *prec
global weathervars=r(varlist)
foreach var in $weathervars {
	rename `var' `var'_month
	foreach lookback in month 270 a365 {
		replace `var'_`lookback'=`var'_`lookback'/10
	
		
		g `var'2_`lookback'=`var'_`lookback'^2
		g `var'3_`lookback'=`var'_`lookback'^3
		g `var'4_`lookback'=`var'_`lookback'^4
	}
}

***to get rid of childage in regressions
replace childage=1
replace dchildage_0=1
replace dchildage_1=1
replace dchildage_2=1
replace dchildage_3=1
replace dchildage_4=1
replace dchildage_99=1


keep if inbcms==1

rename eventviolent_mun eventviolent_mun_month

*Main Table 
	foreach yvar in anyante  antehosp delivhome delivhosp delivother  vaccfull vaccmeas vaccbcg vacchepb vaccdpt vacclastpol {
	local yvarshort=substr("`yvar'",1,9)
	
	if strpos("`yvar'","deliv")>0 {
		local lookback _month
	}
	if strpos("`yvar'","ante")>0 {
		local lookback _270
	}
	if strpos("`yvar'","vacc")>0 {
		local lookback _a365
	}		
		foreach control in basic demogplus {
		global controls ${c_`control'} 
		foreach var in $weatherspec  {
			global controls=subinstr("$controls","`var'","`var'`lookback'",1)
		}
		
				eststo a`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' i.month i.year i.munid  $controls, cluster(munid)
				sum eventviolent_mun`lookback' if e(sample)==1, d
				local xvarmean=round(r(mean),.01)
				local xvarsd=r(sd)*_b[eventviolent_mun`lookback']
				
				estadd ysumm
				estadd local control "`control'"
				estadd local xvarmean "`xvarmean'"
				estadd local xvarsd "`xvarsd'"					

		}
		*control loop	
	}
	*yvar loop end
	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Birth-Related Outcomes Main Results") ///
		se(a2) b(a2) append keep(*eventviolent_mun* ) staraux scalar("ymean Mean of DV" "xvarsd 1SD effect" "control control"   "xvarmean xvarmean" ) 				
	estimates clear	
	
	
	
	*Selection Tests
	foreach xvar in eventviolent_mun_month eventviolent_mun_a365 {
		foreach yvar in $c_selection {
		local yvarshort=substr("`yvar'",1,9)
		
			foreach control in none {
			global controls ${c_`control'} 

					eststo a`yvarshort'`control': reg `yvar' `xvar' i.munid $controls, cluster(munid)
					estadd ysumm
					estadd local control "`control'"
			

			}
			*control loop	
		}
		*yvar loop end
		esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Selection `xvar')") ///
			se(a2) b(a2) append keep(*`xvar'* ) staraux scalar("ymean Mean of DV"   "control control"   ) 				
		estimates clear		
	
	}
	*xvar loop end


*Heterogeneity
	foreach yvar in   delivhome delivhosp   vaccfull vaccmeas  {
	
	if strpos("`yvar'","deliv")>0 {
		local lookback _month
	}
	if strpos("`yvar'","ante")>0 {
		local lookback _270
	}
	if strpos("`yvar'","vacc")>0 {
		local lookback _a365
	}		
	
	local yvarshort=substr("`yvar'",1,9)
		
			foreach control in demogplus   {
			global controls ${c_`control'} 
			foreach var in $weatherspec  {
				global controls=subinstr("$controls","`var'","`var'`lookback'",1)
			}
		
					eststo a`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' c.eventviolent_mun`lookback'#c.(f_tv f_radio) f_tv f_radio i.month i.year i.munid $controls, cluster(munid)
					estadd ysumm
					estadd local control "`control'"				
					
					if strpos("`yvar'","deliv")>0 {
						eststo d`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' c.eventviolent_mun`lookback'#c.(f_tv f_radio) f_tv f_radio i.month i.year i.munid $controls if inlist(year,2013), cluster(munid)
						estadd ysumm
						estadd local control "`control'"	
						estadd local restrict "last year"
						
						eststo d1`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' c.eventviolent_mun`lookback'#c.(f_tv f_radio) f_tv f_radio i.month i.year i.munid $controls if inlist(year,2013, 2012), cluster(munid)
						estadd ysumm
						estadd local control "`control'"	
						estadd local restrict "last 2 years"
						

					}
					
					eststo b`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' c.eventviolent_mun`lookback'#c.(f_tv f_radio) f_tv f_radio c.eventviolent_mun`lookback'#c.wealthscore i.month i.year i.munid $controls, cluster(munid)
					estadd ysumm
					estadd local control "`control'"				

					
					eststo c`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' c.eventviolent_mun`lookback'#c.(ratioany) ratioany i.month i.year i.munid $controls, cluster(munid)
					estadd ysumm
					estadd local control "`control'"								
			}
			*control loop	
		}
		*yvar loop end
		
	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Birth-Related Outcomes Heterogeneity by TV/Radio") ///
		se(a2) b(a2) append order(eventviolent_mun_month eventviolent_mun_a365  *#* f* )  keep(*eventviolent_mun* *f_tv *f_radio ) staraux scalar("ymean Mean of DV" "control control" ) 				
	esttab c* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Birth-Related Outcomes Heterogeneity by Medical Workers") ///
		se(a2) b(a2) append order(eventviolent_mun_month eventviolent_mun_a365  *#* f* ) keep(*eventviolent_mun* *ratioany ) staraux scalar("ymean Mean of DV" "control control" ) 				
	esttab b* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Birth-Related Outcomes Heterogeneity by TV/Radio with Wealth") ///
		se(a2) b(a2) append order(eventviolent_mun_month eventviolent_mun_a365  ratioany *#*) keep(*eventviolent_mun* f_tv f_radio *wealthscore) staraux scalar("ymean Mean of DV" "control control" ) 				
	esttab d* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Birth-Related Outcomes Heterogeneity by TV/Radio: Recent Births") ///
		se(a2) b(a2) append order(eventviolent_mun_month eventviolent_mun_a365  *#* f* )  keep(*eventviolent_mun* *f_tv *f_radio ) staraux scalar("ymean Mean of DV" "control control" "restrict restrict" ) 				

	estimates clear	


*Types of Violence, Alternate Definitions of violence	
	*Delivery
	foreach xvar  in eventviolent_mun_last3 eventshoot_mun eventclash_mun eventkidnap_mun eventbomb_mun pc_eventviolent_mun pa_eventviolent_mun  {
			
		foreach yvar in delivhome delivhosp  {

			local yvarshort=substr("`yvar'",1,9)
	
			foreach control in  demogplus   {
			
			global controls ${c_`control'} 
			local lookback _month
			
			foreach var in $weatherspec  {
				global controls=subinstr("$controls","`var'","`var'`lookback'",1)
			}			

			eststo a`yvarshort'`control': reg `yvar' `xvar' i.month i.year i.munid  $controls, cluster(munid)
			sum `xvar' if e(sample)==1, d
			local xvarmean=round(r(mean),.01)
			local xvarsd=r(sd)*_b[`xvar']
			
			estadd ysumm
			estadd local control "`control'"
			estadd local xvarmean "`xvarmean'"
			estadd local xvarsd "`xvarsd'"	
			}
			*control loop end
		}
		*yvar loop end
		
	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Delivery: `xvar'") ///
		se(a2) b(a2) append keep(*`xvar'* ) staraux scalar("ymean Mean of DV" "xvarsd 1SD effect" "control control"   "xvarmean xvarmean" ) 				
	estimates clear	

	}
	*xvar loop	

	*Vaccinations
	foreach xvar in eventshoot_mun eventclash_mun eventkidnap_mun eventbomb_mun pc_eventviolent_mun pa_eventviolent_mun  {
		local xvarshort=substr("`xvar'",1,7)
			
		foreach yvar in  vaccfull vaccmeas {

			local yvarshort=substr("`yvar'",1,9)
	
			foreach control in  demogplus   {
			global controls ${c_`control'} 
			local lookback _a365
			foreach var in $weatherspec  {
				global controls=subinstr("$controls","`var'","`var'`lookback'",1)
			}			

			eststo a`yvarshort'`control': reg `yvar' `xvar'_a365 i.month i.year i.munid  $controls, cluster(munid)
			sum `xvar'_a365 if e(sample)==1, d
			local xvarmean=round(r(mean),.01)
			local xvarsd=r(sd)*_b[`xvar'_a365]
			
			estadd ysumm
			estadd local control "`control'"
			estadd local xvarmean "`xvarmean'"
			estadd local xvarsd "`xvarsd'"	
			}
			*control loop end
		}
		*yvar loop end
		
	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Vaccinations: `xvar'") ///
		se(a2) b(a2) append keep(*`xvar'* ) staraux scalar("ymean Mean of DV" "xvarsd 1SD effect" "control control"   "xvarmean xvarmean" ) 				
	estimates clear	
	}
	*xvar loop		

	
	
*Robustness - with Additional Controls 
	foreach extralist in storms zhigh c.pctag#c.index  projbuildingxHEA projbuildingxHEA@projbuildingxTRA@projbuildingxEDU    {
	local extralist=subinstr("`extralist'","@"," ",.)
	
		foreach yvar in delivhome delivhosp vaccfull vaccmeas  {
	
			if strpos("`yvar'","deliv")>0 {
				local lookback _month
			}
			if strpos("`yvar'","ante")>0 {
				local lookback _270
			}
			if strpos("`yvar'","vacc")>0 {
				local lookback _a365
			}	
			local yvarshort=substr("`yvar'",1,9)
			
			local extra`lookback' `extralist'
			
			foreach var in `extralist' {
				local var=subinstr("`var'","c.","",.)
				cap g `var'_month= `var'
				local extra`lookback'=subinstr("`extra`lookback''","`var'","`var'`lookback'",1)							
			}
			
			foreach control in demogplus   {
			global controls ${c_`control'} 
			foreach var in $weatherspec  {
				global controls=subinstr("$controls","`var'","`var'`lookback'",1)
			}					
					eststo a`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' `extra`lookback'' i.month i.year i.munid $controls, cluster(munid)
					sum eventviolent_mun`lookback' if e(sample)==1, d
					local xvarmean=round(r(mean),.01)
					local xvarsd=r(sd)*_b[eventviolent_mun`lookback']
					
					estadd ysumm
					estadd local control "`control'"				
					estadd local xvarmean "`xvarmean'"
					estadd local xvarsd "`xvarsd'"
				
			}
			*control loop	
		}
		*yvar loop end
		
	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Birth-Related Outcomes Robustness: With `extralist'") ///
		se(a2) b(a2) append keep(*eventviolent_mun* `extra_month' `extra_a365') order(*eventviolent_mun*) staraux scalar("ymean Mean of DV" "xvarsd 1SD effect" "control control"   "xvarmean xvarmean" ) 				
	estimates clear	
	
	}
	*extra loop end
	
*Leads
	foreach yvar in delivhome delivhosp {
		foreach control in  demogplus   {
		global controls ${c_`control'} 
		foreach var in $weatherspec  {
			global controls=subinstr("$controls","`var'","`var'_month",1)
		}	
				
				eststo a`yvar'`control': reg `yvar' le5_eventviolent_mun  i.month i.year i.munid  $controls , cluster(munid)
				estadd ysumm
				estadd local control "`control'"
				
				eststo a1`yvar'`control': reg `yvar' le5_eventviolent_mun eventviolent_mun_month i.month i.year i.munid  $controls, cluster(munid)
				estadd ysumm
				estadd local control "`control'"

		}
		*control loop	
	}
	*yvar loop end

	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Delivery Placebo") ///
		se(a2) b(a2) append keep(*eventviolent_mun* ) staraux scalar("ymean Mean of DV" "control control" ) 				
	estimates clear	
	
	
	foreach yvar in vaccfull vaccmeas {

		foreach control in  demogplus   {
		global controls ${c_`control'} 
		foreach var in $weatherspec  {
			global controls=subinstr("$controls","`var'","`var'_a365",1)
		}	
				
				eststo a`yvar'`control': reg `yvar' le2_eventviolent_mun_a365  i.month i.year i.munid  $controls , cluster(munid)
				estadd ysumm
				estadd local control "`control'"
				
				eststo a1`yvar'`control': reg `yvar' le2_eventviolent_mun_a365 eventviolent_mun_a365 i.month i.year i.munid  $controls, cluster(munid)
				estadd ysumm
				estadd local control "`control'"

		}
		*control loop	
	}
	*yvar loop end
	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Vaccinations Placebo") ///
		se(a2) b(a2) append keep(*eventviolent_mun* ) staraux scalar("ymean Mean of DV" "control control" ) 				
	estimates clear			
	
	
*With Province Fixed Effects
	foreach yvar in  delivhome delivhosp  vaccfull vaccmeas  {
	local yvarshort=substr("`yvar'",1,9)
	
	if strpos("`yvar'","deliv")>0 {
		local lookback _month
	}
	if strpos("`yvar'","ante")>0 {
		local lookback _270
	}
	if strpos("`yvar'","vacc")>0 {
		local lookback _a365
	}		
		
	foreach control in  demogplus mun {
		global controls ${c_`control'} 
		foreach var in $weatherspec  {
			global controls=subinstr("$controls","`var'","`var'`lookback'",1)
		}
		
				eststo a`yvarshort'`control': reg `yvar' eventviolent_mun`lookback' i.month i.year i.provid  $controls, cluster(munid)
				sum eventviolent_mun`lookback' if e(sample)==1, d
				local xvarmean=round(r(mean),.01)
				local xvarsd=r(sd)*_b[eventviolent_mun`lookback']
				
				estadd ysumm
				estadd local control "`control'"
				estadd local xvarmean "`xvarmean'"
				estadd local xvarsd "`xvarsd'"					

		}
		*control loop	
	}
	*yvar loop end
	esttab a* using "${filename}.csv", star(* 0.10 ** 0.05 *** .01) title("Birth-Related Outcomes Province FE") ///
		se(a2) b(a2) append keep(*eventviolent_mun* ) staraux scalar("ymean Mean of DV" "xvarsd 1SD effect" "control control"   "xvarmean xvarmean" ) 				
	estimates clear	


